import 'package:flutter/material.dart';

class ExpandableSliverPage extends StatefulWidget {
  const ExpandableSliverPage({super.key});

  @override
  State<ExpandableSliverPage> createState() => _ExpandableSliverPageState();
}

class _ExpandableSliverPageState extends State<ExpandableSliverPage> {
  late List<Item> _data;
  late List<bool> _expandedList;

  @override
  void initState() {
    super.initState();
    _data = generateItems(5);
    _expandedList = List<bool>.generate(5, (index) => false);
  }

  List<Item> generateItems(int numberOfItems) {
    return List<Item>.generate(numberOfItems, (int index) {
      return Item(
        headerValue: 'Panel $index',
        expandedValue: 'This is item number $index',
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Expansion Panel Demo'),
      ),
      body: CustomScrollView(
        slivers: [
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return ExpansionPanelList(
                  elevation: 1,
                  expandedHeaderPadding: EdgeInsets.all(0),
                  expansionCallback: (int panelIndex, bool isExpanded) {
                    setState(() {
                      _expandedList[panelIndex] = isExpanded;
                    });
                  },
                  children: [
                    ExpansionPanel(
                      headerBuilder: (BuildContext context, bool isExpanded) {
                        return ListTile(
                          title: Text(_data[index].headerValue),
                        );
                      },
                      body: ListTile(
                        title: Text(_data[index].expandedValue),
                      ),
                      isExpanded: _expandedList[index],
                    ),
                  ],
                );
              },
              childCount: _data.length,
            ),
          ),
        ],
      ),
    );
  }
}

class Item {
  Item({
    required this.headerValue,
    required this.expandedValue,
  });

  String headerValue;
  String expandedValue;
}


// class MyHomePage extends StatefulWidget {
//   @override
//   _MyHomePageState createState() => _MyHomePageState();
// }

// class _MyHomePageState extends State<MyHomePage> {
//   List<Item> _data = [];

//   List<Item> generateItems(int numberOfItems) {
//     return List<Item>.generate(numberOfItems, (int index) {
//       return Item(
//         headerValue: 'Panel $index',
//         expandedValue: 'This is item number $index',
//         isExpanded: false,
//       );
//     });
//   }

//   @override
//   void initState() {
//     super.initState();
//     setState(() {
//       _data = generateItems(5);
//     });
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: Text('Expansion Panel Demo'),
//       ),
//       body: ListView(
//         children: _data.map<Widget>((Item item) {
//           return ExpansionPanelList(
//             elevation: 1,
//             expandedHeaderPadding: EdgeInsets.all(0),
//             expansionCallback: (int index, bool isExpanded) {
//               print('????');
//               setState(() {
//                 item.isExpanded = isExpanded;
//               });
//             },
//             children: [
//               ExpansionPanel(
//                 headerBuilder: (BuildContext context, bool isExpanded) {
//                   return ListTile(
//                     title: Text(item.headerValue),
//                   );
//                 },
//                 body: ListTile(
//                   title: Text(item.expandedValue),
//                 ),
//                 isExpanded: item.isExpanded,
//               ),
//             ],
//           );
//         }).toList(),
//       ),
//     );
//   }
// }

// class Item {
//   Item({
//     required this.expandedValue,
//     required this.headerValue,
//     required this.isExpanded,
//   });

//   String expandedValue;
//   String headerValue;
//   bool isExpanded;
// }
